//
//  TJStyle1Cell.m
//  CrossBorderPayment
//
//  Created by hc-101 on 2020/6/9.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJStyle1Cell.h"

@interface TJStyle1Cell()

@property (weak, nonatomic) UIButton *arrowButton;
@property (weak, nonatomic) UISwitch *switchView;
@property (weak, nonatomic) UIImageView *iconImageView;
/**勾选按钮 */
@property (weak, nonatomic) UIButton *selectButton;


@end

@implementation TJStyle1Cell

- (void)settingSubView {
    
    UIImageView *iconImageView = [UIImageView tj_addToView:self.contentView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.offset(0);
        make.width.height.offset(18);
    }];
    
    UILabel *tj_textLabel = [UILabel lableWithSuperView:self.contentView font:UIFont_Px(16) color:UIColor.blackColor title:@"" textAlignment:NSTextAlignmentLeft];
    [tj_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.offset(0);
    }];
    
    UIButton *arrowButton = [UIButton tj_addToView:self.contentView];
    [arrowButton setImage:[UIImage imageNamed:@"cellArrowIcon"] forState:UIControlStateNormal];
    [arrowButton setImage:[UIImage imageNamed:@"cellArrowIcon"] forState:UIControlStateHighlighted];
    [arrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10);
        make.width.offset(16);
        make.height.offset(16);
        make.centerY.offset(0);
    }];
    
    UISwitch *switchView = [UISwitch tj_addToView:self.contentView];
    switchView.onTintColor = UIColor.tj_mainColor;
    [switchView addTarget:self action:@selector(didClickSwitchView:) forControlEvents:UIControlEventValueChanged];
    [switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(-10);
    }];
    
    
    UIButton *selectButton = [UIButton tj_addToView:self.contentView];
    [selectButton setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
    [selectButton setImage:[UIImage imageNamed:@"select_icon"] forState:UIControlStateSelected];
    [selectButton addTarget:self action:@selector(selectButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.width.offset(20);
        make.height.offset(20);
        make.centerY.offset(0);
    }];
    
    
    UILabel *tj_detailTextLabel = [UILabel lableWithSuperView:self.contentView font:UIFont_Px(14) color:UIColor_Hex(0x9EA0A4) title:@"" textAlignment:NSTextAlignmentRight];
    [tj_detailTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(arrowButton.mas_left).offset(-5);
        make.centerY.offset(0);
        make.left.greaterThanOrEqualTo(tj_textLabel.mas_right);
    }];
    
    [tj_textLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [tj_detailTextLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    
    _tj_textLabel = tj_textLabel;
    _tj_detailTextLabel = tj_detailTextLabel;
    _arrowButton = arrowButton;
    _switchView = switchView;
    _switchView.hidden = YES;
    _iconImageView = iconImageView;
    _iconImageView.hidden = YES;
    _selectButton = selectButton;
    _selectButton.hidden = YES;
}

- (void)didClickSwitchView:(UISwitch *)switchView {
    if (_switchValueChange) _switchValueChange(switchView);
}

- (void)selectButtonClick {
    if (self.selectBtnClickBlock) {
        self.selectBtnClickBlock(self.model);
    }
}
- (void)setModel:(TJCellStyleModel *)model {
    _model = model;
    
    _tj_textLabel.text = model.text;
    _tj_detailTextLabel.text = model.detailText;
    
    //有没有勾选 默认没有
    if (model.hasSelect) {
        self.selectButton.hidden = NO;
        //是否勾选
        if (model.isSelect) {
            
            self.selectButton.selected = YES;
        }else{
            
            self.selectButton.selected = NO;
        }
    }else{
        self.selectButton.hidden = YES;
    }
    
    
    
    if (model.icon) {
        UIImage *iconImage = [UIImage imageNamed:model.icon];
        self.iconImageView.image = iconImage;
        self.iconImageView.hidden = NO;
        [self.iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.centerY.offset(0);
            make.width.offset(iconImage.size.width);
            make.height.offset(iconImage.size.height);
        }];
        
        [self.tj_textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconImageView.mas_right).offset(12);
            make.centerY.offset(0);
        }];
    } else {
        
        self.iconImageView.hidden = YES;
        [self.iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {}];
        
        [self.tj_textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.centerY.offset(0);
        }];
        
    }
    
    if (_model.hasArrow.boolValue) {
        
        self.arrowButton.hidden = NO;
        
        [self.arrowButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-10);
            make.width.offset(16);
            make.height.offset(16);
            make.centerY.offset(0);
        }];
        
        [self.tj_detailTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.arrowButton.mas_left).offset(-5);
            make.centerY.offset(0);
            make.left.greaterThanOrEqualTo(self.tj_textLabel.mas_right);
        }];
    } else {
        self.arrowButton.hidden = YES;
        [self.arrowButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            
        }];
        [self.tj_detailTextLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-10);
            make.centerY.offset(0);
            make.left.greaterThanOrEqualTo(self.tj_textLabel.mas_right);
        }];
    }
    
    if (model.hasSwitch.boolValue) {
        _switchView.hidden = NO;
        _switchView.on = model.isOn.boolValue;
    } else {
        _switchView.hidden = YES;
    }
    
    UIImage *attributedIconImage = [UIImage imageNamed:model.attributedStringIcon];
    CGSize attributedIconSize = model.attributedStringIconSize.CGSizeValue;
    if (attributedIconSize.width <= 0 || attributedIconSize.height <= 0) {
        attributedIconSize = attributedIconImage.size;
    }
    
    if (attributedIconSize.width) {
        
        NSTextAttachment *attchment = [[NSTextAttachment alloc] init];
        attchment.image = attributedIconImage;
        
        
        if (model.isIconINPrevious) {
            
            NSMutableAttributedString *mAtt = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@", model.detailText]];
            attchment.bounds = CGRectMake(0, -2, attributedIconSize.width, attributedIconSize.height);
            [mAtt insertAttributedString:[NSAttributedString attributedStringWithAttachment: attchment] atIndex:0];
            _tj_detailTextLabel.attributedText  = mAtt;
        } else {
            
            NSMutableAttributedString *mAtt = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ", model.detailText]];
            attchment.bounds = CGRectMake(0, -2, attributedIconSize.width, attributedIconSize.height);
            [mAtt appendAttributedString:[NSAttributedString attributedStringWithAttachment: attchment]];
            _tj_detailTextLabel.attributedText  = mAtt;
        }
        
    } else {
        _tj_detailTextLabel.text = model.detailText;
    }
    
}


@end
